package com.tianze.tzkf.utils;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

import java.text.DecimalFormat;

/**
 * Created by JY on 18/12/28.
 * 取apk信息
 */

public class ApkInfoUtil {


    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---getString the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    /**
     * 返回当前程序版本号
     */
    public static int getAppVersionCode(Context context) {
        int code = 0;
        try {
            // ---getString the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            code = pi.versionCode;
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return code;
    }


    /**
     * 返回当前设备AndroidID
     */
    public static String getANDROID_ID(Context c) {
        String phoneAndroidID = "";
        try {
            phoneAndroidID = Settings.Secure.getString(c.getContentResolver(), Settings.Secure.ANDROID_ID)+"";
            Log.e("getANDROID_ID", phoneAndroidID);

        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
        return phoneAndroidID;
    }


    /**
     * 返回当前设备品牌
     */
    public static String getBRAND() {
        String phoneBrand = "";
        try {
            phoneBrand = android.os.Build.BRAND;
        } catch (Exception e) {
            Log.e("BRAND", "Exception", e);
            return "";
        }
        return phoneBrand;
    }

    /**
     * 返回当前设备型号
     */
    public static String getModel() {
        String phoneModel = "";
        try {
            phoneModel = Build.MODEL;
        } catch (Exception e) {
            Log.e("Model", "Exception", e);
            return "";
        }
        return phoneModel;
    }

    /**
     * 返回当前设备系统版本号
     */
    public static String getRELEASE() {
        String phoneRELEASE = "";
        try {
            phoneRELEASE = android.os.Build.VERSION.RELEASE;
        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
        return phoneRELEASE;
    }


    /**
     * 返回当前设备分辨率
     */
    public static String getResolvingPower(Context c) {
        String phoneResolvingPower = "";
        try {
            DisplayMetrics dm = c.getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            phoneResolvingPower = String.valueOf(screenWidth) +"X"+ String.valueOf(screenHeight);
        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
       return phoneResolvingPower;
    }


    /**
     * 返回当前设备内存
     */
    public static String getAvailMAem(Context c) {
        String phoneAvailMAem = "";
        try {
            ActivityManager activityManager=(ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
            ActivityManager.MemoryInfo memInfo=new ActivityManager.MemoryInfo();
            activityManager.getMemoryInfo(memInfo);

            DecimalFormat df = new DecimalFormat("0.00");

            double availMAem_kb = memInfo.availMem/1024;
            //Log.e("kb>",availMAem_kb+">");

            double availMAem_mb = availMAem_kb/1024;
           // Log.e("mb>",availMAem_mb+">");

            double availMAem_G = availMAem_mb/1024;
            //Log.e("G>",availMAem_G+">");

            phoneAvailMAem = df.format(availMAem_G);
        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
        return phoneAvailMAem;
    }


    /**
     * 获取手机国际识别码IMEI
     * */
    public static String getPhoneIMEI(Context context){
        String imei = "";
        try {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "";
        }
        imei = telephonyManager.getSimSerialNumber();
        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
        Log.e("imei", imei+"");

        return imei;

    }

    /**
     * 获取手机SM
     * */
    public static String getPhoneSM(Context context){
        String phoneSM = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return "";
            }
            phoneSM = telephonyManager.getLine1Number();
        } catch (Exception e) {
            Log.e("RELEASE", "Exception", e);
            return "";
        }
        Log.e("phoneSM", phoneSM+"");
        return phoneSM;

    }



    }



